package com.rangers.auth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import com.rangers.auth.base.details.AuthUser;
import com.rangers.auth.base.service.UserService;
import com.rangers.auth.sample.jpa.entity.User;
import com.rangers.auth.sample.jpa.repo.UserRepo;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 系统配置
 *
 * @version 1.0
 * @author fuhw
 * @date 2017年8月17日 下午5:00:41
 */
@Configuration
public class AuthManageConfig {
	
	/**
	 * 
	 * security 安全配置
	 * @version 1.0
	 * @author fuhw
	 * @date 2017年8月17日 下午5:00:53
	 */
	@Configuration
	@EnableWebSecurity
	public static class WebSecurityConfig extends WebSecurityConfigurerAdapter{
		
		@Autowired
		private UserService<User, AuthUser, UserRepo, String> userService;
		
		@Bean
		public PasswordEncoder getPasswordEncoder() {
			return new BCryptPasswordEncoder();
		}
		
		/* (non-Javadoc)
		 * @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder)
		 */
		@Override
		protected void configure(AuthenticationManagerBuilder auth) throws Exception {
			auth.userDetailsService(userService).passwordEncoder(getPasswordEncoder());
		}
		
		// @formatter:off
		/* (non-Javadoc)
		 * @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(org.springframework.security.config.annotation.web.builders.HttpSecurity)
		 */
		@Override
		protected void configure(HttpSecurity http) throws Exception {
			http.httpBasic()
				.and().formLogin().successForwardUrl("/swagger-ui.html")
				.and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).permitAll()
				.and().authorizeRequests().anyRequest().authenticated()
				.and().csrf().disable();
		}
		// @formatter:on
	}
	
	/**
	 * 
	 * swagger2-api 
	 * @version 1.0
	 * @author fuhw
	 * @date 2017年8月21日 上午10:11:29
	 */
	@Configuration
	@EnableSwagger2
	public static class SwaggerConfig {

		@Bean
		public Docket api() {
			// @formatter:off
			return new Docket(DocumentationType.SWAGGER_2)
			          .select()
			          .apis(RequestHandlerSelectors.basePackage("com.rangers.auth.sample.jpa.ctrl"))
			          .paths(PathSelectors.any())
			          .build()
			          .apiInfo(apiInfo());
			// @formatter:on
		}

		private ApiInfo apiInfo() {
			// @formatter:off
		    return new ApiInfoBuilder()
		    		.title("授权管理")
		    		.description("角色用户菜单授权管理 API 文档")
		    		.version("v1.0")
		    		.build();
			// @formatter:on
		}

	}

}
